package com.monkey.apple.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.monkey.apple.R;

import org.javia.arity.Symbols;
import org.javia.arity.SyntaxException;

/**
* Description: 计算器App
* Author: Arthor
* Date: 2017/10/12
* Time: 10:52
*/

public class CalculateActivity extends AppCompatActivity {

    /**
     * UI
     */
    private TextView result;
    private TextView formula;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);

        initView();
    }

    private void initView() {
        result = (TextView) findViewById(R.id.result_area);
        formula = (TextView) findViewById(R.id.formula_area);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_add:
            case R.id.btn_sub:
            case R.id.btn_mul:
            case R.id.btn_div:
            case R.id.btn_dot:
                display(view);
                break;
            case R.id.btn_c:
                clear();
                break;
            case R.id.btn_del:
                delete();
                break;
            case R.id.btn_equ:
                calculate();
                break;
        }
    }

    /**
     * 计算
     */
    private void calculate() {
        String strContent = formula.getText().toString();
        try {
            Symbols s = new Symbols();
            double res = s.eval(strContent);

            result.setText(String.valueOf(res));

            Animation fadeIn = AnimationUtils.loadAnimation(CalculateActivity.this, R.anim.fade_in);
            result.startAnimation(fadeIn);

        } catch (SyntaxException e) {
            Toast.makeText(CalculateActivity.this, "错误!", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 删除最后一个符号
     */
    private void delete() {
        String strContent = formula.getText().toString();
        if (strContent.length() > 0) {
            strContent = strContent.substring(0, strContent.length() - 1);
            formula.setText(strContent);
        }
    }

    /**
     * 清空表达式
     */
    private void clear() {
        formula.setText("");
        result.setText("");
    }

    /**
     * 展示表达式
     */
    private void display(View view) {
        //点击按钮的文本
        String addStr = ((Button) view).getText().toString();
        //历史文本
        String orignalStr = formula.getText().toString();
        //最终表达式
        String finalStr = orignalStr + addStr;
        formula.setText(finalStr);
    }

    /**
     * Activity被销毁时保存数据
     *      注意不是两参的重载方法onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        String strFormula = formula.getText().toString();
        outState.putString("KEY_FORMULA_AREA", strFormula);

        String strResult = result.getText().toString();
        outState.putString("KEY_RESULT_AREA", strResult);
    }

    /**
     * Activity重启时恢复数据
     */
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        String strFormula = savedInstanceState.getString("KEY_FORMULA_AREA");
        formula.setText(strFormula);

        String strResult = savedInstanceState.getString("KEY_RESULT_AREA");
        result.setText(strResult);
    }
}
